$(function(){
	function getTableSource(){
		var tableSource = new $.jqx.dataAdapter({
			dataType: "json",
			dataFields:[
			    { name: "id", type: "int" },
			    { name: "couponName", type: "string" },
			    { name: "amount", type: "double" },
			    { name: "startDate", type: "string" },
			    { name: "endDate", type: "string" },
			    { name: "requirementAmount", type: "double" },
			    { name: "couponNum", type: "int" }
			],
			id: "id",
			url: "coupon/getAllCoupon.do"
		});
		return tableSource;
	}
	
	$("#dataTable").jqxDataTable({
		width: "100%",
        source: getTableSource(),
        pageable: true,
        serverProcessing: true,
        pageSize: 18,
        pagerButtonsCount: 10,
        columnsResize: true,
        sortable: true,
        columns: [
            { text: "序号", width: '16.6%', cellsRenderer: function(row) {  return (row + 1).toString(); } },
            { text: "优惠券名称", dataField: "couponName", width: '14.3%'},
            { text: "金额", dataField: "amount", width: '14.3%' },
            { text: "开始时间", dataField: "startDate", width: '14.3%'},
            { text: "结束时间", dataField: "endDate", width: '14.3%' },
            { text: "满减", dataField: "requirementAmount", width: '14.3%'},
            { text: "剩余张数", dataField: "couponNum", width: '14.3%'}
        ],
	});
	$("#dataTable").on("rowDoubleClick",function(info, args){
		if(args && args.flag){
			info = args;
		}
		editingIndex = info.args.index;
		editingData = info.args.row;
	 	_editerIndex = layer.open({
			id: "sellerEditer",
			title: "查看/修改优惠券信息",
		    type: 1,
		    skin: 'layui-layer-rim',
		    area: ['1000px', '640px'],
		    content:
		    	info.args.type =="add"?
		    	'<form id="coupon-editer" class="form" method="post" style="height: 100%;overflow: auto;">' +
			    '<div id="order_editer" class="form" style="overflow: auto;">' +
			    '<input type="hidden" name="id" />' +
				'<input type="hidden" name="submit-url" value="coupon/addCouponInfo.do" />' +
				'<label class="from-label" for="couponName">优惠券名称：</label>' +
				'<input type="text" name="couponName" allowBlank="false" class="form-control" placeholder="优惠券名称" />' +
				'<label class="from-label" for="amount">金额：</label>' +
				'<input type="text" id="amount" name="amount" allowBlank="false" class="form-control" placeholder="金额" />' +
				'<div class="input-box"><label class="from-label" for="startDate">开始时间：</label>' +
				'<div name="startDate" allowBlank="false" placeholder="开始时间" id="startDate" style="display: inline-block;margin:10px 0;vertical-align:middle"></div></div>' +
				'<div class="input-box"><label class="from-label" for="endDate">结束时间：</label>' +
				'<div name="endDate" allowBlank="false" placeholder="结束时间" id="endDate" style="display: inline-block;margin:10px 0;vertical-align:middle"></div></div>' +
				'<label class="from-label" for="requirementAmount">满减：</label>' +
				'<input type="text" id="requirementAmount" name="requirementAmount" allowBlank="false" class="form-control" placeholder="满减" />' +
				'<label class="from-label" for="couponNum">发行张数：</label>' +
				'<input type="text" id="couponNum" name="couponNum" allowBlank="false" class="form-control" placeholder="发行张数" />' +
				'</div>' +
				'<div id="detailed-datatable"' +
				'	style="border-radius: 0; min-height: 500px; margin: 0 0 0 32px;"></div>' +
				'<br> <br> <br>' +
				'</form>'+
				'<div class="form" style="position: absolute;bottom: 0px;width: 100%;text-align: right;background-color: #DDD;">' +
	    		'<input type="button" class="btn btn-sm btn-success" onclick="submitEditer(this)" value="保存"/>' +
	    		'<input type="button" class="btn btn-sm btn-info" onclick="closeEditer(this, true)" value="返回"/>' +
	    		'</div>':
	    			'<form id="coupon-editer" class="form" method="post" style="height: 100%;overflow: auto;">' +
				    '<div id="order_editer" class="form" style="overflow: auto;">' +
				    '<input type="hidden" name="id" />' +
					'<input type="hidden" name="submit-url" value="coupon/addCouponInfo.do" />' +
					'<label class="from-label" for="couponName">优惠券名称：</label>' +
					'<input type="text" name="couponName" allowBlank="false" class="form-control" placeholder="优惠券名称" />' +
					'<label class="from-label" for="amount">金额：</label>' +
					'<input type="text" id="amount" name="amount" allowBlank="false" class="form-control" placeholder="金额" readonly="true"  />' +
					'<label class="from-label" for="startDate">开始时间：</label>' +
					'<input type="text" name="startDate" allowBlank="false" class="form-control" placeholder="开始时间" readonly="true"  />' +
					'<label class="from-label" for="endDate">结束时间：</label>' +
					'<input type="text" name="endDate" allowBlank="false" class="form-control" placeholder="结束时间" readonly="true"  />' +
					'<label class="from-label" for="requirementAmount">满减：</label>' +
					'<input type="text" id="requirementAmount" name="requirementAmount" allowBlank="false" class="form-control" placeholder="满减" readonly="true"  />' +
					'<label class="from-label" for="couponNum">发行张数：</label>' +
					'<input type="text" id="couponNum" name="couponNum" allowBlank="false" class="form-control" placeholder="发行张数" />' +
					'</div>' +
					'<div id="detailed-datatable"' +
					'	style="border-radius: 0; min-height: 500px; margin: 0 0 0 32px;"></div>' +
					'<br> <br> <br>' +
					'</form>'+
					'<div class="form" style="position: absolute;bottom: 0px;width: 100%;text-align: right;background-color: #DDD;">' +
		    		'<input type="button" class="btn btn-sm btn-info" onclick="closeEditer(this, true)" value="返回"/>' +
		    		'</div>'
	    			
		});
	 	if(info.args.type =="add"){
	 		$("#startDate").jqxDateTimeInput({ width: "200px", height: "34px", formatString: "yyyy-MM-dd", value: null });
			$("#endDate").jqxDateTimeInput({ width: "200px", height: "34px", formatString: "yyyy-MM-dd", value: null });
		 	
	 	}
	 	$("input[name='id']").val(editingData['id']);
	 	setData($("#coupon-editer"), editingData);
	 	setAmounts(editingData);
	});
});

function setAmounts(data){
	$("#amount").val(data.amount);
	$("#requirementAmount").val(data.requirementAmount);
}

/**
 * 是否选中一项
 * 
 * @returns
 */
function isSelected() {
	var rows = $("#dataTable").jqxDataTable("getSelection");
	if (rows.length < 1) {
		layer.msg("请选择一行数据", {
			shift : 6,
			offset : "5%"
		});
		return null;
	}
	return rows;
}